angular.module("app", []).controller("main", ["$http", "$scope", function($http, $scope){
    var radioTM = {
        title: "单选题", //标题
        number: "Q1", //题号
        type: "radio", //类型
        lineStyle: "",
        options: [{ /*选项*/id: 1, /*选项编号*/title: "选项1" /*选项内容*/},{id: 2,title: "选项2"},{id: 3,title: "选项3"}]};
    
    var checkTM = {
        title: "多选题",
        type: "check",
        number: "Q1",
        lineStyle: "",
        options: [{id:1, title: "选项1"}, {id:2, title: "选项2"}, {id:3, title: "选项3"}]
    };
    
    var inputTM = {
        title: "填空题",
        type: "input",
        number: "Q1"
    };
    
    var selectTM = {
        title: "下拉题",
        type: "select",
        number: "Q1",
        options: [{id:1, title: "选项1"}, {id:2, title: "选项2"}, {id:3, title: "选项3"}]
    };

    var pageTM = {
        title: "分隔符",
        type: "page"
    };
    
    //题目类型
    $scope.itemTyps = [{type: "radio", title: "单选题"}, {type: "check", title: "多选题"}, {type: "select", title: "选择题"},{type: "page",title: "分页符"}]; 
    $scope.items = []; //题目
    $scope.lineStyles = [{cssName: "", display: ""}, {cssName: "", display: "垂直一列"}, {cssName: "format2", display: "垂直两列"}, {cssName: "format3", display: "垂直三列"}, {cssName: "format4", display: "垂直四列"}, {cssName: "format5", display: "垂直五列"}, {cssName: "format8", display: "水平"}];
    $scope.currentItem = null; //数据初始化
    
    //默认标题
    $scope.diaochaTitle = "XXX调查";

    //--------------工具函数--------------
    var cloneObject = function(item) { //克隆对象，在新增题目的时候，都是通过这个函数做克隆操作，这里是深度克隆
        var newItem = {};
        var typestr = typeof item;
        if (typestr == "string" || typestr == "number") {
            return item;
        }
        for (var k in item) {
            if (item.hasOwnProperty(k) && k.indexOf("$$") == -1) {
                var v = item[k];
                if (v instanceof Array) {
                    var newVs = [];
                    for (var i = 0, j = v.length; i < j; i++) {
                        var newV = cloneObject(v[i]);
                        newVs.splice(i, 0, newV);
                    }
                    newItem[k] = newVs;
                } else if (typeof v == "object") {
                    newItem[k] = cloneObject(item[k]);   
                } else {
                    newItem[k] = item[k];
                }
            }
        }
        return newItem;
    };
    
    Array.prototype.indexOf = function(item) {
        var result = -1;
        for (var i = 0, j = this.length; i < j; i++) {
            if (this[i] == item) {
                result = i;
                break;
            }
        }
        return result;  
    };
    
    //--------------业务代码--------------
    $scope.oldTitleState = true;
    $scope.newTitleState = !$scope.oldTitleState;
    $scope.newTitle = $scope.diaochaTitle;
    function toggleTitle(){
        $scope.oldTitleState = !$scope.oldTitleState;
        $scope.newTitleState = !$scope.newTitleState;
    }
    $scope.changeTitle = function() {
        toggleTitle();
    }
    $scope.updateTitle = function(val){
        $scope.newTitle = val;
    };

    $scope.cancelTitle = function() {
        toggleTitle();
    }
    $scope.saveTite = function(){
        toggleTitle();
        $scope.diaochaTitle = $scope.newTitle;
    }
    $scope.cancelTitle = function(){
        toggleTitle();
        $scope.newTitle = $scope.diaochaTitle;
    }
    $scope.selectItem = function(item) {
        $scope.currentItem = item;
    };
    
    $scope.deleteItem = function(item){
        if (!confirm("您真的要删除问题：" + item.title + "?")) return;
        var items = $scope.items;
        var pos = items.indexOf(item);
        if (pos != -1)
            items.splice(pos, 1);
    };
    
    $scope.cloneItem = function(item, index) {
        var newItem = cloneObject(item);
        $scope.items.splice(index + 1, 0, newItem);
    }
    
    $scope.addOption = function(item) {
        var items = $scope.currentItem.options;
        var newItem = cloneObject(item);
        var index = items.indexOf(item);
        $scope.currentItem.options.splice(index + 1, 0, newItem);
    };
    
    $scope.delOption = function(item) {
        if (!confirm("您真的要删除选项：" + item.title + "?")) return;
        var items = $scope.currentItem.options;
        debugger;
        var pos = items.indexOf(item);
        if (pos != -1)
            items.splice(pos, 1);
    };
    
    $scope.addRadio = function() {
        var pos = $scope.items.indexOf($scope.currentItem);
        $scope.items.splice(pos + 1, 0, cloneObject(radioTM));
    };
    
    $scope.addCheckbox = function() {
        var pos = $scope.items.indexOf($scope.currentItem);
        $scope.items.splice(pos + 1, 0, cloneObject(checkTM));
    };
    
    $scope.addInput = function() {
        var pos = $scope.items.indexOf($scope.currentItem);
        $scope.items.splice(pos + 1, 0, cloneObject(inputTM));
    };
    
    $scope.addSelect = function() {
        var pos = $scope.items.indexOf($scope.currentItem);
        $scope.items.splice(pos + 1, 0, cloneObject(selectTM));
    };
    $scope.addPage = function() {
        var pos = $scope.items.indexOf($scope.currentItem);
        $scope.items.splice(pos + 1, 0, cloneObject(pageTM));
    };

    window.onDraged = function(typeStr) {
        var typemap = {
            'radio' : radioTM,
            'check' : checkTM,
            'select' : selectTM,
            'input' : inputTM,
            'page' : pageTM
        }
        $scope.items.push(cloneObject(typemap[typeStr]));
        if (!$scope.$$phase)
            $scope.$apply();
    };
    
    void function main() {
        $scope.items.push(cloneObject(radioTM)); // 默认先放一个单选题
    }();
}]);

$(function(){
    $("#question-type-box li.move").draggable({
        connectToSortable: "#question-box",
        snapTolerance: 300,
        helper: function() {
            return $('<div class="draggable-holder">' + $(this).html() + "</div>")
        },
        stop: function() {
            var type = $(this).data('type');
            window.onDraged(type);
        }
    });
})